package com.logos.service.impl;

import com.logos.common.ResultCode;
import com.logos.dao.BookDao;
import com.logos.entity.Book;
import com.logos.exception.SystemException;
import com.logos.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
    public boolean insert(Book book) {
        return bookDao.save(book) > 0;
    }

    @Override
    public boolean update(Book book) {
        return bookDao.update(book) > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return bookDao.delete(id) > 0;
    }

    @Override
    public Book selectById(Integer id) {
        // 将可能出现的异常进行包装，转换成自定义异常
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new SystemException(ResultCode.SYSTEM_TIMEOUT_ERR, "服务器访问超时，请重试", e);
        }
        return bookDao.selectById(id);
    }

    @Override
    public List<Book> selectAll() {
        return bookDao.selectAll();
    }
}
